package pailaidai.hc.com.mysvgdemo.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.CountDownTimer;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import pailaidai.hc.com.mysvgdemo.R;
import pailaidai.hc.com.mysvgdemo.view.CustomProgressDialog;
import pailaidai.hc.com.mysvgdemo.view.MyProgress;

public class CameraActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView camera_iamge;
    private Button camera_button2,camera_button;
    private CustomProgressDialog dialog;
    private MyProgress myProgress;
    private CountDownTimer downTimer=new Time(5000,1000);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        inintView();
        initListenter();
        staIntentCamera();

    }



    private void inintView() {
        dialog=new CustomProgressDialog.Builder(this).create();
        myProgress=MyProgress.createDialog(this,"上传中",false,null);


        camera_button= (Button) findViewById(R.id.camera_button);
        camera_button2= (Button) findViewById(R.id.camera_button2);
        camera_iamge= (ImageView) findViewById(R.id.camera_image);



    }
    private void initListenter() {
        camera_button.setOnClickListener(this);
        camera_button2.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
       switch (view.getId()){
           case R.id.camera_button:
               myProgress.show();
//               dialog.show();
               downTimer.start();
               break;
           case R.id.camera_button2:
               dialog.dismiss();
               break;
       }

    }

    private void staIntentCamera(){
        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent,1000);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            Bundle bundle = data.getExtras();
            Bitmap bitmap= (Bitmap) bundle.get("data");
           camera_iamge.setImageBitmap(bitmap);
        }
    }

    class  Time extends CountDownTimer{
        public Time(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {

        }

        @Override
        public void onFinish() {
            myProgress.dismiss();
//             dialog.dismiss();
        }
    }

}
